package com.github.data_manage.controller

import com.github.data_manage.common.CommonResult
import com.github.data_manage.domain.vo.ContactEmailVo
import com.github.data_manage.domain.vo.SendEmailVo
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import jakarta.annotation.PostConstruct
import org.springframework.mail.SimpleMailMessage
import org.springframework.mail.javamail.JavaMailSender
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController


@RestController
@RequestMapping("/email")
@Tag(name = "SendEmailController", description = "发送邮件")
open class SendEmailController(
    private val mailSender: JavaMailSender,
) {


    @PostMapping("/send")
    @Operation(description = "发送邮件")
    fun sendEmail(@RequestBody sendEmailVo: SendEmailVo): CommonResult<String> {
        val message = SimpleMailMessage()
        message.subject = sendEmailVo.subject
        message.text = sendEmailVo.body
        message.setTo(sendEmailVo.to)
        message.from = "funddatamanage@gmail.com"
        mailSender.send(message)
        return CommonResult.success("发送成功")
    }

    @PostMapping("/contact")
    @Operation(description = "发送邮件")
    fun contactEmail(@RequestBody contactEmailVo: ContactEmailVo): CommonResult<String> {
        val message = SimpleMailMessage()
        message.subject = "New Submission"
        message.text = """
            You've just received a new submission.
            
            Name: ${contactEmailVo.name}
            Email: ${contactEmailVo.email}
            Message: ${contactEmailVo.message}
        """.trimIndent()
        message.setTo("IR@spark-i.com")
        message.from = "funddatamanage@gmail.com"
//        message.setTo("prentice@163.com")
//        message.from = "1806632927@qq.com"
        mailSender.send(message)
        return CommonResult.success("success")
    }

}
